//
//  HOUploadImageView.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/5/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOUploadImageView.h"
#import<SDWebImage/UIButton+WebCache.h>
#import "UIImageView+WebCache.h"

@protocol HOSingleImageViewDelegate;
@interface HOSingleImageView : UIView
@property(nonatomic, retain)UIButton *removeBtn;
@property(nonatomic, retain)UIImageView *imgView;
@property(nonatomic, retain)UIButton *imageViewBtn;
@property(nonatomic, retain)UIImage *imageData;
@property(nonatomic, assign)int index;
@property(nonatomic, assign)id<HOSingleImageViewDelegate> delegate;
@property (nonatomic, copy)SDWebImageCompletionBlock downloadBlock;
- (void)setImagePath:(NSString *)path;
- (void)showRemoveButton:(BOOL)showRemoveButton;
@end


@protocol HOSingleImageViewDelegate <NSObject>

- (void)removeImage:(HOSingleImageView *)imageView;
- (void)imageView:(HOSingleImageView *)imageView downloadedImage:(UIImage *)image;
- (void)viewOrigionImage:(HOSingleImageView *)imageView withIndex:(int)index;

@end

static UIImage *defaultImage;

@implementation HOSingleImageView
- (instancetype)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if(self)
	{
		if(!defaultImage)
			[UIImage imageNamed:@"morentu"];
		[self initSubviews];
		self.downloadBlock = ^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
		{
			[self.delegate imageView:self downloadedImage:image];
		};
	}
	return self;
}

- (void)initSubviews
{
	self.imageViewBtn = [[UIButton alloc]
						 initWithFrame:CGRectMake(0,
												  0,
												  self.frame.size.width,
												  self.frame.size.height)];
	[self.imageViewBtn addTarget:self action:@selector(imageDidClick:) forControlEvents:UIControlEventTouchUpInside];
	[self.imageViewBtn setBackgroundImage:defaultImage forState:UIControlStateNormal];
	[self addSubview:self.imageViewBtn];
	
	self.imgView = [[UIImageView alloc]
				 initWithFrame:CGRectMake(0,
										  0,
										  self.frame.size.width,
										  self.frame.size.height)];
	[self addSubview:self.imgView];
	self.imgView.hidden = YES;
	
	
	self.removeBtn = [[UIButton alloc]
					  initWithFrame:CGRectMake(self.frame.size.width - 15,
											   0,
											   15,
											   15)];
	[self.removeBtn addTarget:self action:@selector(removeImage:) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:self.removeBtn];
	[self.removeBtn setBackgroundImage:[UIImage imageNamed:@"icon_baoxiuxiangqing_shanchu"] forState:UIControlStateNormal];
	
}

- (void)updateImage
{
	if(self.imageData)
		[self.imageViewBtn setBackgroundImage:self.imageData forState:UIControlStateNormal];
}
- (void)setImagePath:(NSString *)path
{
	if(path == nil)
		return;
	self.removeBtn.hidden = YES;
	[self.imageViewBtn sd_setImageWithURL:[NSURL URLWithString:path]
								 forState:UIControlStateNormal
						 placeholderImage:[UIImage imageNamed:@"morentu"]
								  options:SDWebImageRefreshCached
								completed:self.downloadBlock];
}

- (void)showRemoveButton:(BOOL)showRemoveButton
{
	self.removeBtn.hidden = !showRemoveButton;
}

- (void)removeImage:(id)sender
{
	[self.delegate removeImage:self];
}

- (void)imageDidClick:(id)sender
{
	[self.delegate viewOrigionImage:self withIndex:self.index];
}

- (void)dealloc
{
	[self.removeBtn removeFromSuperview];
	[self.imageViewBtn removeFromSuperview];
	self.delegate = nil;
//	self.downloadBlock = nil;
	[self.removeBtn release];
	self.removeBtn = nil;
	[self.imageViewBtn release];
	self.imageViewBtn = nil;
	self.imageData = nil;
	self.imgView.image = nil;
	[self.imgView release];
	self.imgView = nil;
	[super dealloc];
}

@end


@interface HOUploadImageView()<HOSingleImageViewDelegate, UIActionSheetDelegate>
{
	UIButton *addImageBtn;
	
}
//@property(nonatomic, retain)NSMutableArray *imageList;
@property(nonatomic, assign)CGFloat singleWidth;
@property(nonatomic, retain)NSMutableArray *selectedImageList;
@property(nonatomic, retain)NSArray *imagePathList;
@property(nonatomic, assign)BOOL isDownloadModal;
@property(nonatomic, retain)NSMutableDictionary *imageDic;
@end

@implementation HOUploadImageView
@synthesize delegate;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if(self)
	{
		[self initDataSource];
		[self initSubviews];
	}
	return self;
}

- (void)initSubviews
{
	addImageBtn = [[UIButton alloc] initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
															 0,
															 self.singleWidth,
															 self.singleWidth)];
	[addImageBtn setBackgroundImage:[UIImage imageNamed:@"ic_baoxiuxiangqing_tianjiatupian"] forState:UIControlStateNormal];
	[addImageBtn addTarget:self action:@selector(toAddImage:) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:addImageBtn];
}


- (void)toAddImage:(id *)sender
{
	if(self.delegate)
	{
		[self.delegate takePhoto:nil];
	}
}

- (void)initDataSource
{
	self.singleWidth = self.frame.size.height;
	self.selectedImageList = [[[NSMutableArray alloc] init] autorelease];
	self.imageDic = [[[NSMutableDictionary alloc] init] autorelease];
}

- (void)imageView:(HOSingleImageView *)imageView downloadedImage:(UIImage *)image
{
	if(imageView == nil || image == nil)
		return;
	if(image)
	{
		[self fillInImage:image withImgView:imageView];
	}
}

- (void)fillInImage:(UIImage *)image withImgView:(HOSingleImageView *)imageView
{
	if(image == nil || imageView  == nil)
		return;
	UIImage *oldImage = [self.imageDic objectForKey:[imageView description]];
	[self.selectedImageList removeObject:oldImage];
	
	[self.imageDic setObject:image forKey:[imageView description]];
	[self.selectedImageList addObject:image];
	imageView.imageData = image;
}

- (void)updateImageWithList:(NSArray *)imageList
{
	
	self.isDownloadModal = NO;
	NSArray *subviews = [NSArray arrayWithArray:self.subviews];
	for(UIView *subview in subviews)
	{
		if(addImageBtn != subview)
		{
			[subview removeFromSuperview];
		}
	}
	[self.imageDic removeAllObjects];
	for(int i = 0; i < [imageList count]; i ++)
	{
		UIImage *img = [imageList objectAtIndex:i];
		HOSingleImageView *singleView = [self createSingleViewWithImage:img];
		singleView.index = i;
		[self fillInImage:img withImgView:singleView];
		[singleView showRemoveButton:self.needDisplayRemoveButton];
		[self addSubview:singleView];
		singleView.frame = CGRectMake(HO_HORIZONAL_PADDING + i *(self.singleWidth + HO_HORIZONAL_PADDING),
												 0,
												 self.singleWidth,
												 self.singleWidth);
//		if(img)
//		{
//			[self.imageDic setObject:img forKey:[singleView description]];
//
//		}
		
	}
	if(self.selectedImageList.count == 3)
	{
		addImageBtn.hidden = YES;
	}
	else
	{
		addImageBtn.hidden = NO;
		addImageBtn.frame = CGRectMake(HO_HORIZONAL_PADDING + self.selectedImageList.count *(self.singleWidth + HO_HORIZONAL_PADDING),
										0,
										addImageBtn.frame.size.width,
										addImageBtn.frame.size.height);
	}
}

- (void)setImagePaths:(NSArray *)array
{
	if(array == nil)
		return;
	self.isDownloadModal = YES;
	
	NSArray *subviews = [NSArray arrayWithArray:self.subviews];
	for(UIView *subview in subviews)
	{
		if(addImageBtn != subview)
		{
			[subview removeFromSuperview];
		}
	}
	self.imagePathList = [NSArray arrayWithArray:array];
	if(array == nil || [array count] == 0)
		return;
	for(int i = 0; i < [array count]; i ++)
	{
		NSString *path = [array objectAtIndex:i];
		HOSingleImageView *singleView = [self createSingleViewWithPath:path];
		singleView.index = i;
		[self addSubview:singleView];
		[singleView showRemoveButton:self.needDisplayRemoveButton];
		singleView.frame = CGRectMake(HO_HORIZONAL_PADDING + i *(self.singleWidth + HO_HORIZONAL_PADDING),
									  0,
									  self.singleWidth,
									  self.singleWidth);
	}
	if(self.selectedImageList.count == 3)
	{
		addImageBtn.hidden = YES;
	}
	else
	{
		addImageBtn.hidden = NO;
		addImageBtn.frame = CGRectMake(HO_HORIZONAL_PADDING + self.selectedImageList.count *(self.singleWidth + HO_HORIZONAL_PADDING),
									   0,
									   addImageBtn.frame.size.width,
									   addImageBtn.frame.size.height);
	}

}

- (void)hideUploadButton
{
	[addImageBtn removeFromSuperview];
}

- (HOSingleImageView *)createSingleViewWithImage:(UIImage *)img
{
	HOSingleImageView *singleView = [[[HOSingleImageView alloc]
									 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
															  0,
															  self.singleWidth,
															  self.frame.size.height)] autorelease];
	singleView.delegate = self;
	singleView.imageData = img;
	singleView.layer.borderWidth = 1;
	
	[singleView updateImage];
	return singleView;
}


- (HOSingleImageView *)createSingleViewWithPath:(NSString *)path
{
	HOSingleImageView *singleView = [[[HOSingleImageView alloc]
									  initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
															   0,
															   self.singleWidth,
															   self.frame.size.height)] autorelease];
	singleView.delegate = self;
	[singleView setImagePath:path];
//	singleView.layer.borderWidth = 1;
	[singleView updateImage];
	return singleView;
}

- (void)viewOrigionImage:(HOSingleImageView *)imageView withIndex:(int)index
{
	if(self.delegate)
	{
		if(self.isDownloadModal)
		{
			[self.delegate viewFullImage:self.imagePathList withCurrentIndex:index];
		}
		else
		{
			[self.delegate viewFullImage:self.selectedImageList withCurrentIndex:index];
		}
	}
}

- (void)addImage:(NSArray *)array
{
	if([array count] == 0)
		return;
	NSMutableArray *imageList = [NSMutableArray arrayWithArray:self.selectedImageList];
	for(int i = 0; i < [array count]; i ++)
	{
		UIImage *img = [array objectAtIndex:i];
		[imageList addObject:img];
	}
	[self.selectedImageList removeAllObjects];
	[self updateImageWithList:imageList];
}

- (void)removeImage:(HOSingleImageView *)imageView
{
	UIImage *img = imageView.imageData;
	[self.selectedImageList removeObject:img];
	[self.imageDic removeObjectForKey:[imageView description]];
	imageView.imageData = nil;
	NSArray *copyImagelist = [NSArray arrayWithArray:self.selectedImageList];
	[self.selectedImageList removeAllObjects];
	[self updateImageWithList:copyImagelist];
}

- (NSArray *)getImages
{
	return self.selectedImageList;
}
- (NSInteger)getRemainPhotoCount
{
	return 3 - [self.selectedImageList count];
}
- (void)cleanup
{
	[self.selectedImageList removeAllObjects];
	[self updateImageWithList:self.selectedImageList];
}

- (void)dealloc
{
	[self.selectedImageList removeAllObjects];
	self.selectedImageList = nil;
	[self.imageDic removeAllObjects];
	self.imageDic = nil;
	self.delegate = nil;
	[addImageBtn removeFromSuperview];
	[addImageBtn release];
	addImageBtn = nil;
	[super dealloc];
}
@end
